package com.woniuxy.dr_mall.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.woniuxy.dr_mall.entity.DrCart;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/**
 * <p>
 * Mapper 接口
 * </p>
 *
 * @author 张哲轩
 * @since 2021-08-11
 */
public interface DrCartMapper extends BaseMapper<DrCart> {
    /**
     * 根据用户id查购物车里的商品信息
     *
     * @param userId 用户id
     * @return 购物车商品list
     * @author yy
     */
    @Select("select * from dr_cart where userid=#{userId}")
    @Results({
            @Result(column = "skuid", property = "skus", one = @One(
                    fetchType = FetchType.LAZY,
                    select = "com.woniuxy.dr_mall.mapper.DrSpecPriceMapper.getSkusBySkuId"
            ))

    })
    List<DrCart> getCartsByUserId(int userId);

    /**
     * 根据主键查商品信息
     *
     * @param id    主键
     * @return  购物车里的商品信息
     */
    @Select("select * from dr_cart where id=#{id}")
    @Results({
            @Result(id = true, column = "id", property = "id"),
            @Result(column = "userId", property = "userId"),
            @Result(column = "price", property = "price"),
            @Result(column = "ckecked", property = "ckecked"),
            @Result(column = "skuid", property = "skus", one = @One(
                    fetchType = FetchType.LAZY,
                    select = "com.woniuxy.dr_mall.mapper.DrSpecPriceMapper.getSkusBySkuId"
            ))
    })
    DrCart getCartsById(int id);

    /**
     * 根据用户id清空购物车
     *
     * @param userId 用户id
     * @author yy
     * @since 2021-8-14 9:45
     */
    @Delete("delete from dr_cart where userid=#{userId}")
    void clearCart(int userId);
}
